/*     */ package com.zimbra.cs.store;
/*     */ 
/*     */ import com.zimbra.common.localconfig.KnownKey;
/*     */ import com.zimbra.common.localconfig.LC;
/*     */ import com.zimbra.common.util.Log;
/*     */ import com.zimbra.common.util.ZimbraLog;
/*     */ import java.io.File;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Arrays;
/*     */ import java.util.Collection;
/*     */ import java.util.Collections;
/*     */ import java.util.List;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class IncomingDirectory
/*     */ {
/*     */   private String mPath;
/*     */   private UniqueFileNameGenerator mNameGenerator;
/*     */   private static final long SWEEP_INTERVAL_MS = 60000L;
/*     */   private static IncomingDirectorySweeper mSweeper;
/*     */   
/*     */   public IncomingDirectory(String path)
/*     */   {
/*  34 */     this.mPath = path;
/*  35 */     this.mNameGenerator = new UniqueFileNameGenerator();
/*     */   }
/*     */   
/*     */   public String getPath() {
/*  39 */     return this.mPath;
/*     */   }
/*     */   
/*     */   void setPath(String path) {
/*  43 */     this.mPath = path;
/*     */   }
/*     */   
/*     */   public File getNewIncomingFile() {
/*  47 */     return new File(this.mPath + File.separator + this.mNameGenerator.getFilename());
/*     */   }
/*     */   
/*     */   private static class UniqueFileNameGenerator {
/*     */     private long mTime;
/*     */     private long mSequence;
/*     */     
/*  54 */     public UniqueFileNameGenerator() { reset(); }
/*     */     
/*     */     private void reset()
/*     */     {
/*  58 */       this.mTime = System.currentTimeMillis();
/*  59 */       this.mSequence = 0L;
/*     */     }
/*     */     
/*     */     public String getFilename() { long time;
/*     */       long sequence;
/*  64 */       synchronized (this) {
/*  65 */         if (this.mSequence >= 1000L)
/*  66 */           reset();
/*  67 */         time = this.mTime;
/*  68 */         sequence = this.mSequence++;
/*     */       }
/*  70 */       return time + "-" + sequence + ".msg";
/*     */     }
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static synchronized void startSweeper()
/*     */   {
/*  80 */     if (mSweeper != null) {
/*  81 */       return;
/*     */     }
/*  83 */     long sweepMaxAgeMS = LC.zimbra_store_sweeper_max_age.intValue() * 60 * 1000;
/*  84 */     IncomingDirectorySweeper sweeper = new IncomingDirectorySweeper(60000L, sweepMaxAgeMS);
/*  85 */     sweeper.start();
/*  86 */     mSweeper = sweeper;
/*     */   }
/*     */   
/*     */   public static synchronized void stopSweeper() {
/*  90 */     if (mSweeper == null) {
/*  91 */       return;
/*     */     }
/*  93 */     mSweeper.signalShutdown();
/*     */     try {
/*  95 */       mSweeper.join();
/*     */     } catch (InterruptedException e) {}
/*  97 */     mSweeper = null;
/*     */   }
/*     */   
/*     */   public static synchronized void setSweptDirectories(IncomingDirectory inc) {
/* 101 */     IncomingDirectorySweeper.sSweptDirectories = Arrays.asList(new IncomingDirectory[] { inc });
/*     */   }
/*     */   
/*     */   public static synchronized void setSweptDirectories(Collection<IncomingDirectory> swept) {
/* 105 */     if (swept == null) {
/* 106 */       IncomingDirectorySweeper.sSweptDirectories = Collections.emptyList();
/*     */     } else {
/* 108 */       IncomingDirectorySweeper.sSweptDirectories = new ArrayList(swept);
/*     */     }
/*     */   }
/*     */   
/*     */   private static class IncomingDirectorySweeper extends Thread {
/*     */     static List<IncomingDirectory> sSweptDirectories;
/* 114 */     private boolean mShutdown = false;
/*     */     private long mSweepIntervalMS;
/*     */     private long mMaxAgeMS;
/*     */     
/*     */     public IncomingDirectorySweeper(long sweepIntervalMS, long maxAgeMS) {
/* 119 */       super();
/* 120 */       setDaemon(true);
/* 121 */       this.mSweepIntervalMS = sweepIntervalMS;
/* 122 */       this.mMaxAgeMS = maxAgeMS;
/*     */     }
/*     */     
/*     */     public synchronized void signalShutdown() {
/* 126 */       this.mShutdown = true;
/* 127 */       wakeup();
/*     */     }
/*     */     
/*     */     public synchronized void wakeup() {
/* 131 */       notify();
/*     */     }
/*     */     
/*     */     public void run() {
/* 135 */       ZimbraLog.store.info(getName() + " thread starting");
/*     */       
/* 137 */       boolean shutdown = false;
/* 138 */       long startTime = System.currentTimeMillis();
/*     */       
/* 140 */       while (!shutdown)
/*     */       {
/* 142 */         synchronized (this) {
/* 143 */           if (!this.mShutdown) {
/* 144 */             long now = System.currentTimeMillis();
/* 145 */             long until = startTime + this.mSweepIntervalMS;
/* 146 */             if (until > now) {
/*     */               try {
/* 148 */                 wait(until - now);
/*     */               } catch (InterruptedException e) {}
/*     */             }
/*     */           }
/* 152 */           shutdown = this.mShutdown;
/* 153 */           if (shutdown) {
/*     */             break;
/*     */           }
/*     */         }
/* 157 */         int numDeleted = 0;
/* 158 */         startTime = System.currentTimeMillis();
/*     */         
/*     */ 
/* 161 */         for (IncomingDirectory inc : sSweptDirectories) {
/* 162 */           File directory = new File(inc.getPath());
/* 163 */           if (directory.exists())
/*     */           {
/* 165 */             File[] files = directory.listFiles();
/* 166 */             if (files != null)
/*     */             {
/*     */ 
/* 169 */               for (int i = 0; i < files.length; i++)
/*     */               {
/* 171 */                 if (i % 100 == 0) {
/* 172 */                   synchronized (this) {
/* 173 */                     shutdown = this.mShutdown;
/*     */                   }
/* 175 */                   if (shutdown) {
/*     */                     break;
/*     */                   }
/*     */                 }
/* 179 */                 File file = files[i];
/* 180 */                 if (!file.isDirectory())
/*     */                 {
/*     */ 
/* 183 */                   long lastMod = file.lastModified();
/*     */                   
/*     */ 
/* 186 */                   if (lastMod > 0L)
/*     */                   {
/*     */ 
/* 189 */                     long age = startTime - lastMod;
/* 190 */                     if (age >= this.mMaxAgeMS) {
/* 191 */                       boolean deleted = file.delete();
/* 192 */                       if (!deleted)
/*     */                       {
/*     */ 
/* 195 */                         if (file.exists())
/* 196 */                           ZimbraLog.store.warn("Sweeper unable to delete " + file.getAbsolutePath());
/* 197 */                       } else if (ZimbraLog.store.isDebugEnabled()) {
/* 198 */                         ZimbraLog.store.debug("Sweeper deleted " + file.getAbsolutePath());
/* 199 */                         numDeleted++;
/*     */                       }
/*     */                     }
/*     */                   }
/*     */                 } }
/* 204 */               synchronized (this) {
/* 205 */                 shutdown = this.mShutdown;
/*     */               }
/* 207 */               if (shutdown) break;
/*     */             }
/*     */           }
/*     */         }
/* 211 */         long elapsed = System.currentTimeMillis() - startTime;
/*     */         
/* 213 */         ZimbraLog.store.debug("Incoming directory sweep deleted " + numDeleted + " files in " + elapsed + "ms");
/*     */       }
/*     */       
/* 216 */       ZimbraLog.store.info(getName() + " thread exiting");
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/store/IncomingDirectory.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */